package com.castle.fortress.admin.exchange.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.castle.fortress.admin.core.constants.GlobalConstants;
import com.castle.fortress.admin.exchange.entity.ExchangeConfigEntity;
import com.castle.fortress.admin.exchange.dto.ExchangeConfigDto;
import com.castle.fortress.admin.exchange.enums.CodeTypeEnum;
import com.castle.fortress.admin.exchange.enums.PostageTypeEnum;
import com.castle.fortress.admin.exchange.enums.VoucherStatusEnum;
import com.castle.fortress.admin.exchange.service.ExchangeConfigService;
import com.castle.fortress.common.enums.YesNoEnum;
import com.castle.fortress.common.utils.ConvertUtil;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.GlobalRespCode;
import com.castle.fortress.common.enums.OperationTypeEnum;
import com.castle.fortress.admin.core.annotation.CastleLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;
import org.apache.logging.log4j.util.Strings;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.castle.fortress.admin.utils.ExcelUtils;
import javax.servlet.http.HttpServletResponse;
import com.castle.fortress.common.entity.DynamicExcelEntity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 兑换中心设置 控制器
 *
 * @author whc
 * @since 2022-12-05
 */
@Api(tags="兑换中心设置管理控制器")
@Controller
public class ExchangeConfigController {
    @Autowired
    private ExchangeConfigService exchangeConfigService;

    /**
     * 兑换中心设置的分页展示
     * @param exchangeConfigDto 兑换中心设置实体类
     * @param current 当前页
     * @param size  每页记录数
     * @return
     */
    @CastleLog(operLocation="兑换中心设置",operType = OperationTypeEnum.QUERY)
    @ApiOperation("兑换中心设置分页展示")
    @GetMapping("/exchange/exchangeConfig/page")
    @ResponseBody
    @RequiresPermissions("exchange:exchangeConfig:pageList")
    public RespBody<IPage<ExchangeConfigDto>> pageExchangeConfig(ExchangeConfigDto exchangeConfigDto, @RequestParam(required = false) Integer current, @RequestParam(required = false)Integer size){
        Integer pageIndex= current==null? GlobalConstants.DEFAULT_PAGE_INDEX:current;
        Integer pageSize= size==null? GlobalConstants.DEFAULT_PAGE_SIZE:size;
        Page<ExchangeConfigDto> page = new Page(pageIndex, pageSize);
        IPage<ExchangeConfigDto> pages = exchangeConfigService.pageExchangeConfigExtends(page, exchangeConfigDto);

        return RespBody.data(pages);
    }

    /**
     * 兑换中心设置的列表展示
     * @param exchangeConfigDto 兑换中心设置实体类
     * @return
     */
    @CastleLog(operLocation="兑换中心设置",operType = OperationTypeEnum.QUERY)
    @ApiOperation("兑换中心设置列表展示")
    @GetMapping("/exchange/exchangeConfig/list")
    @ResponseBody
    public RespBody<List<ExchangeConfigDto>> listExchangeConfig(ExchangeConfigDto exchangeConfigDto){
        List<ExchangeConfigDto> list = exchangeConfigService.listExchangeConfig(exchangeConfigDto);
        return RespBody.data(list);
    }

    /**
     * 兑换中心设置保存
     * @param exchangeConfigDto 兑换中心设置实体类
     * @return
     */
    @CastleLog(operLocation="兑换中心设置",operType = OperationTypeEnum.INSERT)
    @ApiOperation("兑换中心设置保存")
    @PostMapping("/exchange/exchangeConfig/save")
    @ResponseBody
    @RequiresPermissions("exchange:exchangeConfig:save")
    public RespBody<String> saveExchangeConfig(@RequestBody ExchangeConfigDto exchangeConfigDto){
        if(exchangeConfigDto == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        ExchangeConfigEntity exchangeConfigEntity = ConvertUtil.transformObj(exchangeConfigDto,ExchangeConfigEntity.class);
        if(exchangeConfigService.save(exchangeConfigEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 兑换中心设置编辑
     * @param exchangeConfigDto 兑换中心设置实体类
     * @return
     */
    @CastleLog(operLocation="兑换中心设置",operType = OperationTypeEnum.UPDATE)
    @ApiOperation("兑换中心设置编辑")
    @PostMapping("/exchange/exchangeConfig/edit")
    @ResponseBody
    @RequiresPermissions("exchange:exchangeConfig:edit")
    public RespBody<String> updateExchangeConfig(@RequestBody ExchangeConfigDto exchangeConfigDto){
        if(exchangeConfigDto == null || exchangeConfigDto.getId() == null || exchangeConfigDto.getId().equals(0L)){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        ExchangeConfigEntity exchangeConfigEntity = ConvertUtil.transformObj(exchangeConfigDto,ExchangeConfigEntity.class);
        if(exchangeConfigService.updateById(exchangeConfigEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 兑换中心设置删除
     * @param id
     * @return
     */
    @CastleLog(operLocation="兑换中心设置",operType = OperationTypeEnum.DELETE)
    @ApiOperation("兑换中心设置删除")
    @PostMapping("/exchange/exchangeConfig/delete")
    @ResponseBody
    @RequiresPermissions("exchange:exchangeConfig:delete")
    public RespBody<String> deleteExchangeConfig(@RequestParam Long id){
        if(id == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if(exchangeConfigService.removeById(id)) {
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }


    /**
     * 兑换中心设置批量删除
     * @param ids
     * @return
     */
    @CastleLog(operLocation="兑换中心设置",operType = OperationTypeEnum.DELETE)
    @ApiOperation("兑换中心设置批量删除")
    @PostMapping("/exchange/exchangeConfig/deleteBatch")
    @ResponseBody
    @RequiresPermissions("exchange:exchangeConfig:deleteBatch")
    public RespBody<String> deleteExchangeConfigBatch(@RequestBody List<Long> ids){
        if(ids == null || ids.size()<1){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if(exchangeConfigService.removeByIds(ids)) {
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 兑换中心设置详情
     * @param id 兑换中心设置id
     * @return
     */
    @CastleLog(operLocation="兑换中心设置",operType = OperationTypeEnum.QUERY)
    @ApiOperation("兑换中心设置详情")
    @GetMapping("/exchange/exchangeConfig/info")
    @ResponseBody
    @RequiresPermissions("exchange:exchangeConfig:info")
    public RespBody<ExchangeConfigDto> infoExchangeConfig(@RequestParam Long id){
        if(id == null){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        ExchangeConfigDto exchangeConfigDto =  exchangeConfigService.getByIdExtends(id);

        return RespBody.data(exchangeConfigDto);
    }

	/**
     * 动态表头导出 依据展示的字段导出对应报表
     * @param dynamicExcelEntity
     * @param response
     * @throws Exception
     */
    @CastleLog(operLocation="兑换中心设置",operType = OperationTypeEnum.EXPORT)
	@PostMapping("/exchange/exchangeConfig/exportDynamic")
	@ApiOperation("动态表头导出，依据展示的字段导出对应报表")
	public void exportDynamic(@RequestBody DynamicExcelEntity<ExchangeConfigDto> dynamicExcelEntity, HttpServletResponse response) throws Exception {
		List<ExchangeConfigDto> list = exchangeConfigService.listExchangeConfig(dynamicExcelEntity.getDto());
		//字典、枚举、接口、json常量等转义后的列表数据 根据实际情况初始化该对象，null为list数据直接导出
		List<List<Object>> dataList = null;

        Map<String, Map<String, Object>> convertMap = new HashMap<>();
        //枚举处理 selectEnum 字段使用枚举（DataPermissionPostEnum） 转义
        Map<String, Object> enumTranslateMap = new HashMap<>();
        for (CodeTypeEnum e : CodeTypeEnum.values()) {
            enumTranslateMap.put(e.getCode().toString(), e.getName());
        }

        Map<String, Object> postTypeMap = new HashMap<>();
        for (PostageTypeEnum e : PostageTypeEnum.values()) {
            postTypeMap.put(e.getCode().toString(), e.getName());
        }
        Map<String, Object> YesNo = new HashMap<>();
        for (YesNoEnum e : YesNoEnum.values()) {
            YesNo.put(e.getCode().toString(), e.getName());
        }
        convertMap.put("type", enumTranslateMap);
        convertMap.put("isBind", YesNo);
        convertMap.put("postageType", postTypeMap);
        dataList = ExcelUtils.convertToDataList(list,dynamicExcelEntity.getHeaderList(),convertMap,null);
		ExcelUtils.exportDynamic(response,dynamicExcelEntity.getFileName()+".xlsx",null,list,dynamicExcelEntity.getHeaderList(),dataList);
	}


}
